<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Question extends CI_Controller {
	

	public function index() {
	}

	public function getQuestions() {

		$ids = $this->input->post('ids');
		if(!$ids) return "";
		
		$this->load->model('Model_question');
		$cursor = $this->Model_question->getQuestionsArr($ids);
		$cursor->sort(array('question'=>1));

		// Conver mongoCursor to JSON
		$arr = array();
		foreach(iterator_to_array($cursor) as $qts) {
			array_push($arr, $qts);
		}
		echo json_encode($arr);	
	}

	public function getAdvanceQuestions() {		
		$this->load->model('Model_question');
		$cursor = $this->Model_question->getAdvanceQuestions();
		echo json_encode(iterator_to_array($cursor));	
	}
	public function getAdvanceQuestion($id) {		
		$this->load->model('Model_question');
		$cursor = $this->Model_question->getAdvanceQuestion($id);
		echo json_encode($cursor);	
	}
	public function getQsByEvent($idEv) {
		$this->load->model('Model_question');
		$cursor = $this->Model_question->getQsByEvent($idEv);
		echo json_encode(iterator_to_array($cursor));	
	}

	// =============================================================================
	// =============================================================================
	// =============================================================================
	public function getOptQuestions() {
		$this->load->model('Model_question');
		$qts = $this->Model_question->get_opt_question();
		$qts = iterator_to_array($qts);

		$this->load->model('Model_category');
		$cats = $this->Model_category->getSections();
		$cats = iterator_to_array($cats);

		$arr = array();
		foreach ($cats as $cat) {
			$cat['questions'] = array();
			foreach($qts as $q) {
				if($q['category'] == $cat['_id']) {
					array_push($cat['questions'], $q);
				}
			}
			array_push($arr, $cat);
		}
		echo json_encode($arr);
	}
}

?>